
package com.netease.xone.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewFlipper;

import com.netease.xone.R;
import com.netease.xone.widget.tabview.AbsTabView.OnTabSelectListener;
import com.netease.xone.widget.tabview.MyMessageTab;

/**
 * 我的信息
 *
 * @author MR
 */
public class FragmentMyMessage extends FragmentBase implements IFragmentMain{

    private static final String TAG = "FragmentMyMessage";

    private FragmentMessageBubble mFragmentBubble;
    private MyMessageTab mTab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View view = inflater.inflate(R.layout.fragment_my_message, container, false);
        findViews(view);
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if(mFragmentBubble != null && mTab != null){
            if(isVisibleToUser && mTab.getCurrentIndex() == 0){
                mFragmentBubble.setUserVisibleHint(true);
            }else{
                mFragmentBubble.setUserVisibleHint(false);
            }
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void findViews(View root) {
        final ViewFlipper viewFlipper = (ViewFlipper)root.findViewById(R.id.my_message_flipper);
        mFragmentBubble = FragmentMessageBubble.newInstance();
        FragmentProfile fragmentProfile = FragmentProfile.newInstance(true);
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.replace(R.id.my_message_container1, mFragmentBubble);
        ft.replace(R.id.my_message_container2, fragmentProfile);
        ft.commit();
        mTab = (MyMessageTab)root.findViewById(R.id.my_message_tab);
        mTab.setOnTabSelectListener(new OnTabSelectListener() {

            @Override
            public void onTabSelected(View tabView, int index) {
                if(index == 0){
                    mFragmentBubble.setUserVisibleHint(true);
                }else{
                    mFragmentBubble.setUserVisibleHint(false);
                }
                viewFlipper.setDisplayedChild(index);
            }

            @Override
            public void onTabReSelected(View tabView, int index) {

            }
        });
    }

    @Override
    public void onPageSelected(int index) {
        getActivity().setTitle(R.string.main_tab_me);
    }

    @Override
    public void onPageReSelected(int index) {

    }

}
